import { httpInstance } from "@/utils/httpInstance";

export type ChannelType = { id: string, name: string };

// 获取频道列表API
export const getChannelListAPI = () => {
    const controller = new AbortController();
    const request = httpInstance.request<{ channels: Array<ChannelType> }>({ method: 'GET', url: '/channels', signal: controller.signal });
    return { request, controller };
};

export type ArticleType = {
    channel_id: string,
    content: string,
    title: string,
    cover: { type: number, images: Array<string> },
    pub_date?: string,
};

// 发布文章API
export const publishArticleAPI = (article: ArticleType) => {
    const controller = new AbortController();
    const request = httpInstance.request({
        method: 'POST',
        url: '/mp/articles?draft=false',
        data: article,
        signal: controller.signal,
    });
    return { request, controller };
};

export type SelectArticleParamsType = {
    page?: number,
    per_page?: number,
    begin_pubdate?: string,
    end_pubdate?: string,
    status?: number,
    channel_id?: number
};

export type ArticleItemType = {
    id: string,
    title: string,
    pubdate: string,
    cover: { images: Array<string>, type: number },
    status: number,
    comment_count: number,
    like_count: number,
    read_count: number,
};

// 获取文章API
export const selectArticleAPI = (params?: SelectArticleParamsType) => {
    const controller = new AbortController();
    const request = httpInstance.request<{
        page: number,
        per_page: number,
        total_count: number,
        results: Array<ArticleItemType>,
    }>({
        method: 'GET',
        url: '/mp/articles',
        params,
        signal: controller.signal,
    });
    return { request, controller };
};

// 删除文章API
export const deleteArticleAPI = (id: string) => {
    const controller = new AbortController();
    const request = httpInstance.request({
        method: 'DELETE',
        url: `/mp/articles/${id}`,
        signal: controller.signal,
    });
    return { controller, request };
};

// 查询文章数据API
export const selectArticleDataAPI = (id: string) => {
    const controller = new AbortController();
    const request = httpInstance.request<ArticleType>({
        method: 'GET',
        url: `/mp/articles/${id}`,
        signal: controller.signal,
    });
    return { request, controller };
};

// 更新文章API
export const updateArticleAPI = (id: string, article: ArticleType) => {
    const controller = new AbortController();
    const request = httpInstance.request({
        method: 'PUT',
        url: `/mp/articles/${id}?draft=false`,
        data: article,
        signal: controller.signal,
    });
    return { request, controller };
};